package net.dipper.tankwar.game.control;

import net.dipper.tankwar.core.config.BackgroundItem;
import net.dipper.tankwar.core.data.ObjectMap;
import net.dipper.tankwar.core.helper.Position;

public class MoveDownCommand extends GameControlCommand {

	public MoveDownCommand(GameControl parent, ObjectMap map) {
		super(parent, map);
	}

	@Override
	public void exec() {
		Position player = map.getPlayer();
		if(map.getDirection(player.x, player.y) != ObjectMap.DIRECTION_S){
			map.setDirection(player.x, player.y, ObjectMap.DIRECTION_S);
			return;
		}
		
		int y = player.y + 1;
		if(y >= map.getHeight())
			return;
		
		byte value = map.getObject(player.x, y);
		if(value == 0 && parent.canOperate()){
			byte back = map.getBackground(player.x, y);
			BackgroundItem item = (BackgroundItem) parent.getConfig().getBackground(back);
			if(item == null || !item.getIsCrossable())
				return;
			
			map.move(player.x, player.y, player.x, y, ObjectMap.DIRECTION_S);
			map.setPlayer(player.x, y);
			map.setDirection(player.x, y, ObjectMap.DIRECTION_S);
			parent.setLastOperateTimeStamp(System.currentTimeMillis());
			parent.resetViewPort();
		}
	}

}
